package com.xl.web.controller.dict;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.alibaba.druid.util.StringUtils;
import com.xl.web.controller.fns.FnsBaseUtil;
import com.xl.web.entity.dict.DictBase;

/**
 * 
 * @author yangjian
 *
 */
public class DictSelectTag extends TagSupport  {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String dictType;
    private boolean defaultValue;
    private String value;
    private String name;
    private String id;
    private String cssClass;
    private String styleClass;
    private String multiple;
    private String onChange;
    private String display="edit";

    
    
    public String getCssClass() {
        return cssClass;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public String getMultiple() {
        return multiple;
    }

    public void setMultiple(String multiple) {
        this.multiple = multiple;
    }

    public String getOnChange() {
        return onChange;
    }

    public void setOnChange(String onChange) {
        this.onChange = onChange;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getDictType() {
		return dictType;
	}

	public void setDictType(String dictType) {
		this.dictType = dictType;
	}

	public boolean isDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(boolean defaultValue) {
        this.defaultValue = defaultValue;
    }
    public String getDisplay() {
		return display;
	}

	public void setDisplay(String display) {
		this.display = display;
	}

	@Override
    public int doEndTag() throws JspException{
		  JspWriter out = pageContext.getOut();
	      StringBuffer sb = new StringBuffer();
		if("edit".equals(display)){
			List<DictBase> dict_list = FnsBaseUtil.getDictList(dictType);
	        sb.append("<select name='"+this.getName()+"' id='"+this.getId()+"'");
	        if(!StringUtils.isEmpty(this.getCssClass())){
	            sb.append("class=\"" + this.getCssClass() + "\" ");
	        }
	        if(!StringUtils.isEmpty(this.getStyleClass())){
	            sb.append("style=\"" + this.getStyleClass() + "\" ");
	        }
	        if(!StringUtils.isEmpty(this.getMultiple())){
	            sb.append("multiple=\"" + this.getMultiple() + "\" ");
	        }
	        if(!StringUtils.isEmpty(this.getOnChange())){
	            sb.append("onchange=\"" + this.getOnChange() + "\" ");
	        }
	        sb.append(">");
	        if(this.isDefaultValue()){  
	            sb.append("<option value=''>--请选择--</option>");  
	        }
	        for(DictBase dc:dict_list){
	            if(dc.getDictValue().equals(this.getValue())){
	                sb.append("<option value='"+dc.getDictValue()+"' selected='selected'>");
	            }else{
	                sb.append("<option value='"+dc.getDictValue()+"'>");
	            }
	            sb.append(dc.getDictName()+"</option>");
	        }
	        sb.append("</select>");
		}else if("view".equals(display)){
			String label=FnsBaseUtil.getDictLabel(dictType, value);
			sb.append(label);
		}
        
        try {
            out.write(sb.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new JspException(e);
        }
        return TagSupport.EVAL_PAGE;
    }
    
}
